/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.service.impl;

import com.opgea.qms.dao.CompanyDAO;
import com.opgea.qms.dao.DestinationDAO;
import com.opgea.qms.dao.EmployeeDAO;
import com.opgea.qms.dao.LoginDAO;
import com.opgea.qms.domain.entities.Company;
import com.opgea.qms.domain.entities.Destination;
import com.opgea.qms.domain.entities.Employee;
import com.opgea.qms.domain.entities.Login;
import com.opgea.qms.domain.entities.LoginPK;
import com.opgea.qms.domain.qualifiers.LoginQualifier;
import com.opgea.qms.service.EmployeeService;
import com.opgea.qms.web.form.EmployeeDTO;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ramesh
 */
@Service
public class EmployeeServiceImpl implements EmployeeService{

    
    @Autowired
    private EmployeeDAO employeeDAO;
    @Autowired
    private CompanyDAO companyDAO;
    @Autowired
    private DestinationDAO destinationDAO;
    @Autowired
    private LoginDAO loginDAO;
    
    @Override
    public EmployeeDTO create(EmployeeDTO employeeDTO) {
        Employee employee = new Employee();
        Company company = companyDAO.find(employeeDTO.getCompanyId());
        Destination destination = destinationDAO.find(employeeDTO.getDestinationId());
        
        employee.setFirstName(employeeDTO.getFirstName());
        employee.setMiddleInitial(employeeDTO.getMiddleInitial());
        employee.setLastName(employeeDTO.getLastName());
        employee.setEmpCode(employeeDTO.getEmpCode());
        employee.setEmailId(employeeDTO.getEmailId());
        employee.setPhoneNo(employeeDTO.getPhone1());
        employee.setCompany(company);
        employee.setDestination(destination);

        employeeDAO.create(employee);
        
        /*
        LoginPK loginPK = new LoginPK();
        loginPK.setCompanyId(company.getId());
        loginPK.setLoginId(employeeDTO.getEmailId());
        */
        
        Login login = new Login();
        login.setLoginId(employeeDTO.getEmailId());
        login.setPassword("default");
        login.setLoginQualifier(LoginQualifier.Primary.ordinal());
        login.setEmployee(employee);
        
        loginDAO.create(login);

        return employeeDTO;
    }

    @Override
    public EmployeeDTO update(EmployeeDTO employeeDTO) {
        Employee employee = null;
        if(employeeDTO.getId() != null){
            Destination destination = destinationDAO.find(employeeDTO.getDestinationId());
            employee = employeeDAO.find(employeeDTO.getId());
            employee.setId(employeeDTO.getId());
            employee.setFirstName(employeeDTO.getFirstName());
            employee.setMiddleInitial(employeeDTO.getMiddleInitial());
            employee.setLastName(employeeDTO.getLastName());
            employee.setEmpCode(employeeDTO.getEmpCode());
            employee.setEmailId(employeeDTO.getEmailId());
            employee.setPhoneNo(employeeDTO.getPhone1());
            employee.setDestination(destination);
            employeeDAO.update(employee);
        }else{
            this.create(employeeDTO);
        }
        
        return employeeDTO;
    }

    @Override
    public List<EmployeeDTO> findAll() {
        List<Employee> empList = employeeDAO.findAll();
        List<EmployeeDTO> empDTOList = new ArrayList<EmployeeDTO>();
        for(Employee employee : empList){
            EmployeeDTO empDTO = new EmployeeDTO();
            empDTO.setId(employee.getId());
            empDTO.setFirstName(employee.getFirstName());
            empDTO.setMiddleInitial(employee.getMiddleInitial());
            empDTO.setLastName(employee.getLastName());
            empDTO.setEmailId(employee.getEmailId());
            empDTO.setEmpCode(employee.getEmpCode());
            empDTO.setDestinationId(employee.getDestination().getId());
            empDTO.setDestinationName(employee.getDestination().getName());
            empDTO.setPhone1(employee.getPhoneNo());
            empDTO.setPhone2("No Data");
            empDTOList.add(empDTO);
            //System.out.println("EmployeeDTO : "+empDTO);
        }
        return empDTOList;
    }

    @Override
    public List<EmployeeDTO> findAllByCompanyId(Long companyId) {
        List<Employee> empList = employeeDAO.findAllByCompanyId(companyId);
        List<EmployeeDTO> empDTOList = new ArrayList<EmployeeDTO>();
        for(Employee employee : empList){
            EmployeeDTO empDTO = new EmployeeDTO();
            empDTO.setId(employee.getId());
            empDTO.setFirstName(employee.getFirstName());
            empDTO.setMiddleInitial(employee.getMiddleInitial());
            empDTO.setLastName(employee.getLastName());
            empDTO.setEmailId(employee.getEmailId());
            empDTO.setEmpCode(employee.getEmpCode());
            if(employee.getDestination() != null){
                empDTO.setDestinationId(employee.getDestination().getId());
                empDTO.setDestinationName(employee.getDestination().getName());
            }
            empDTO.setPhone1(employee.getPhoneNo());
            empDTO.setPhone2("No Data");
            empDTOList.add(empDTO);
            //System.out.println("EmployeeDTO : "+empDTO);
        }
        return empDTOList;
    }
    
    @Override
    public Employee find(Long employeeId) {
        return employeeDAO.find(employeeId);
    }

    @Override
    public Employee delete(Long employeeId) {
        return employeeDAO.delete(employeeId);
    }

}
